package com.gyyx.plugins;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

/**
 * Created by zxn on 2020/2/11.
 */
public class NotificationHelper {
    private static final NotificationHelper ourInstance = new NotificationHelper();
    private static final String TAG = "NotificationHelper";
    private static final Handler mHandler = new Handler(Looper.getMainLooper());
//    private GeneralNotification mNotification;
//    private NotificationManager mNotificationManager;

//    public static NotificationHelper getInstance() {
//        return ourInstance;
//    }
//    private NotificationHelper() {
//
//
//    }

    /**
     * @param context
     * @param title
     * @param content
     * @param delayMillis 秒数 *1000
     */
    public static void showNotification(final Context context,
                                 final int icon,
                                 final String title,
                                 final String content,
                                 int delayMillis) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent();
                String action = context.getPackageName();
                Log.i(TAG, "action: " + action);
                intent.setAction(action);
                PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                GeneralNotification mNotification = new GeneralNotification.Builder(context)
                        .icon(icon)
                        .setContentTitle(title)
                        .setContentText(content)
                        .notificationId(100)
                        .setAutoCancel(true)
                        .setContentIntent(mPendingIntent)
                        .build();
                mNotification.sendNotify();
            }
        }, delayMillis * 1000);

    }

    public static void clearNotification(Context context) {
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (null != mNotificationManager){
            mNotificationManager.cancelAll();
        }
    }
}
